package com.jwdf.uda.ui.fragment.question.index;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.event.IndividualEvent;
import com.jwdf.uda.event.NewProblemsEvent;
import com.jwdf.uda.event.QuizNetErrorEvent;
import com.jwdf.uda.event.ReplyDetailEvent;
import com.jwdf.uda.event.ReplyDetailItemClickEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.action.QuizIndexAction;
import com.jwdf.uda.ui.adapter.GenericQuizAdapter;
import com.jwdf.uda.ui.fragment.base.SimpleListFragment;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.NETUtils;

import java.util.List;

import retrofit2.Response;

/**
 * Created by fatsfan on 2016/9/7.
 */
@SuppressLint("ValidFragment")
public class QuizIndexFragment extends SimpleListFragment {

    protected Activity aty;
    protected View layEmpty;
    protected PbBean.PbSignBaseQuestionType restType = PbBean.PbSignBaseQuestionType.QUESTION_INDEX_NONE;
    protected View mErrorView;
    private GenericQuizAdapter mAdapter;

    @Override
    protected void initViews() {
        mErrorView = createNetErrorView();
        mAdapter = new GenericQuizAdapter(getActivity());
        lv.setAdapter(mAdapter);
        layEmpty = mDataBinding.tvEmptyData;
        aty = getActivity();
        EventBusUtil.registerEvent(this);
    }

    @Override
    protected void onListItemClick(int position) {

    }

    @Override
    protected void requestData(final boolean refresh) {

        if (refresh && mAdapter.isEmpty()) {
            showLoadingPage();
        }

        int offset = refresh ? 0 : mAdapter.getCount();
        onNewRequestCall(QuizIndexAction.newInstance(getContext(), offset, restType).request(new AHttpService.IResCallback<PbBean.PbResQuestionQuestionList>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResQuestionQuestionList> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (getView() != null) {
                    //                    if(HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                    //                        mDataBinding.commonFragmentList.stopRefresh();
                    //                        mDataBinding.commonFragmentList.stopLoadMore();
                    //                        return;
                    //                    }
                    if (response != null) {
                        doSuccessResponse(refresh, response.body());
                    } else {
                        if (resultCode != AHttpService.IResCallback.RESULT_OK && !NETUtils.isNetworkConnected(getContext())) {
                            showLoadingPage();
                            setLoadFailedMessage(R.string.loading_failed_message);
                            mDataBinding.commonFragmentList.stopRefresh();
                            mDataBinding.commonFragmentList.stopLoadMore();
                            EventBusUtil.post(new QuizNetErrorEvent());
                        }
                    }
                }
            }
        }));
    }

    protected void doSuccessResponse(boolean refresh, PbBean.PbResQuestionQuestionList body) {
        if (body != null) {
            closeLoadingPage();
            removeNetErrorView();
            final List<PbBean.PbBaseQuestion> questionInfoList = body.getQuestionInfoList();
            if (refresh) {
                mAdapter.setData(questionInfoList);
            } else {
                mAdapter.append(questionInfoList);
            }

            if (questionInfoList.size() == 0)
                mDataBinding.commonFragmentList.setEmptyView(layEmpty);

            mDataBinding.commonFragmentList.setPullLoadEnable(questionInfoList.size() == getPageLength());
            mDataBinding.commonFragmentList.stopRefresh();
            mDataBinding.commonFragmentList.stopLoadMore();
        }
    }

    private void removeNetErrorView() {
        ViewGroup parent = (ViewGroup) mErrorView.getParent();
        if (parent != null) {
            parent.removeView(mErrorView);
        }
    }

    private View createNetErrorView() {
        View view = getActivity().getLayoutInflater().inflate(R.layout.view_net_error, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        requestData(true);
    }

    @Override
    public void onDestroyView() {
        EventBusUtil.unregisterEvent(this);
        super.onDestroyView();
    }

    public void onEventMainThread(ReplyDetailEvent event) {
//        if (ReplyDetailEvent.EVENT_DO_REPLY_PRAISE == event.getEvent()) {
//            requestData(true);
//        }
    }

    public void onEventMainThread(ReplyDetailItemClickEvent event) {
        if (ReplyDetailItemClickEvent.EVENT_COMMENT_LIKE_CLICK == event.getEvent()) {
            requestData(true);
        }
    }

    public void onEventMainThread(NewProblemsEvent event) {
        switch (event.getEvent()) {
            case NewProblemsEvent.EVENT_COMMENT_DELETE:
            case NewProblemsEvent.EVENT_COMMENT_RELEASE:
                requestData(true);
                break;
        }
    }

    public void onEventMainThread(IndividualEvent event) {
        requestData(true);
    }
}
